package com.wgchao.authmanager.infrastructure.repository;

import com.wgchao.authmanager.domain.model.AuthoInfo;
import com.wgchao.authmanager.domain.model.RoleAutho;
import com.wgchao.authmanager.domain.model.RoleIdentity;
import com.wgchao.authmanager.domain.repository.RoleAuthoRepository;
import com.wgchao.authmanager.infrastructure.jpa.entity.RoleAuthoHandleInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.RoleAuthoInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.RoleAuthoHandleJpaRepository;
import com.wgchao.authmanager.infrastructure.jpa.repository.RoleAuthoInfoJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @ClassName: RoleAuthoRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/5/9 4:57 PM
 * @Description: TODO
 */
@Component
public class RoleAuthoRepositoryImpl implements RoleAuthoRepository {
    @Autowired
    private RoleAuthoInfoJpaRepository raJpaRepository;
    @Autowired
    private RoleAuthoHandleJpaRepository rahJpaRepository;

    @Override
    public void add(RoleAutho roleAutho) {
        //删除原有的授权
        List<RoleAuthoInfo> roleAuthoInfos = raJpaRepository.findAllByAgencyCodeAndRoleName(
                roleAutho.getRoleIdentity().getAgencyCode(), roleAutho.getRoleIdentity().getRoleName());
        if(roleAuthoInfos != null){
            for(RoleAuthoInfo roleAuthoInfo : roleAuthoInfos){
                List<RoleAuthoHandleInfo> roleAuthoHandleInfos = rahJpaRepository
                        .findAllByRoleAuthoId(roleAuthoInfo.getId());
                if(roleAuthoHandleInfos!=null && !roleAuthoHandleInfos.isEmpty()){
                    rahJpaRepository.deleteAll(roleAuthoHandleInfos);
                }
            }
            raJpaRepository.deleteAll(roleAuthoInfos);
            rahJpaRepository.findAll();
            raJpaRepository.findAll();
        }
        //重新写入授权
        for(AuthoInfo authoInfo : roleAutho.getAuthoInfos()){
            RoleAuthoInfo roleAuthoInfo = new RoleAuthoInfo();
            roleAuthoInfo.setAgencyCode(roleAutho.getRoleIdentity().getAgencyCode());
            roleAuthoInfo.setRoleName(roleAutho.getRoleIdentity().getRoleName());
            roleAuthoInfo.setProjectIdentify(authoInfo.getProjectIdentify());
            roleAuthoInfo.setMenuIdentify(authoInfo.getMenuIdentify());
            roleAuthoInfo.setCreateTime(roleAutho.getOperator().getOperateTime());
            roleAuthoInfo.setCreateUser(roleAutho.getOperator().getUname());
            roleAuthoInfo = raJpaRepository.save(roleAuthoInfo);

            for(String handle : authoInfo.getHandles()){
                RoleAuthoHandleInfo roleAuthoHandleInfo = new RoleAuthoHandleInfo();
                roleAuthoHandleInfo.setHandleIdentify(handle);
                roleAuthoHandleInfo.setRoleAuthoId(roleAuthoInfo.getId());
                roleAuthoHandleInfo = rahJpaRepository.save(roleAuthoHandleInfo);
            }
        }
    }

    @Override
    public void remove(RoleAutho roleAutho, String operator) {

    }

    @Override
    public RoleAutho get(RoleIdentity id) {
        return null;
    }
}
